package com.yiqi.lottery.feature.find.view;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.widget.AdvancedWebView;
import com.yiqi.lottery.entity.bo.FindScoreBo;
import com.yiqi.lottery.feature.find.contract.LiveContract;
import com.yiqi.lottery.feature.find.dependencies.live.DaggerLiveComponent;
import com.yiqi.lottery.feature.find.dependencies.live.LivePresenterModule;

import javax.inject.Inject;

public class ScoreLiveActivity extends BaseActivity implements LiveContract.View , AdvancedWebView.Listener{


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_find_basketball)
    TextView mBtnFindBasketball;
    @BindView(R.id.btn_find_football)
    TextView mBtnFindFootball;
    @BindView(R.id.webview)
    AdvancedWebView mWebview;
    @Inject
    LiveContract.Presenter mPresenter;
    @BindView(R.id.view)
    LinearLayout mView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_score_live;
    }

    @Override
    protected void initInjector() {
        DaggerLiveComponent.builder().livePresenterModule(new LivePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        mPresenter.getScoreLive(1);
    }


    @Override
    protected void initView() {
        mBtnFindBasketball.setBackground(getResources().getDrawable(R.drawable.bg_find_score_left_select));
        initToolBar(mToolbar, "", true, true);
        mWebview.setListener(this,this);
    }


    @OnClick({R.id.btn_find_basketball, R.id.btn_find_football})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_find_basketball:
                mPresenter.getScoreLive(1);
                mBtnFindBasketball.setBackground(getResources().getDrawable(R.drawable.bg_find_score_left_select));
                mBtnFindFootball.setBackground(getResources().getDrawable(R.drawable.bg_find_score_left_unselect));
                break;
            case R.id.btn_find_football:
                mPresenter.getScoreLive(2);
                mBtnFindBasketball.setBackground(getResources().getDrawable(R.drawable.bg_find_score_right_unselect));
                mBtnFindFootball.setBackground(getResources().getDrawable(R.drawable.bg_find_score_right_select));
                break;
        }
    }


    @Override
    public void showScoreSuccess(FindScoreBo url) {
        mWebview.loadUrl(url.getUrl());
    }
    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        if (mWebview!=null)
           mWebview.onResume();

    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        if (mWebview!=null)
        mWebview.onPause();
        // ...
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (mWebview!=null)
        mWebview.onDestroy();
        super.onDestroy();
    }
    @Override


    public void showScoreLiveError(String error) {
        T.showShort(error);
    }

    @Override
    public void onPageStarted(String url, Bitmap favicon) {
        showLoading();

    }

    @Override
    public void onPageFinished(String url) {
       dismissLoading();
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        T.showShort(description);
        dismissLoading();
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {

    }

    @Override
    public void onExternalPageRequest(String url) {

    }
}
